package com.hxf.web.action.meal;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.PublicUtil;
import com.hxf.dto.MealBoxDto;
import com.hxf.dto.RestaurantDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class MealboxAction extends WebActionBase  { 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String images;
	private Integer type;
	private Integer peopleNumber;
	private Double lowConsume; 
	private String remark;
	private Integer status = 0;

	private List<MealBoxDto> mealBoxList; 
	private MealBoxDto mealBoxDto;
	private RestaurantDto restaurantDto;
	
	/**
	 * 餐厅包厢
	 * @return
	 */
	public String mealBoxList() {
		MealBoxDto mbx = new MealBoxDto();
		try {
			mbx.setName(name);
			mbx.setPeopleNumber(peopleNumber);
			mbx.setLowConsume(lowConsume);
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session
			 
			//判断是否管理员登陆还是餐厅管理员登陆
			if(null != adminUserDto){
				mbx.setRestaurantId(getAdminUser().getId());
			}else if(null != restaurentUserDto){
				mbx.setRestaurantId(Integer.valueOf(getSession().getAttribute("restaurantId").toString()));
			}
		
			ReturnData<MealBoxDto> resultData = super.serviceFacade.getMealBoxService().findByMealBoxPage(mbx, targetPage, pageSize);
			mealBoxList = resultData.getResultlist();
			setRecordCount(resultData.getRecordcount());
			
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "meal-box-list";
	} 
	
	/**
	 * 添加包厢信息
	 * @return
	 */
	public String mealBox() { 
		try {
			if (id != null && id != 0) {
				mealBoxDto = (MealBoxDto)super.serviceFacade.getMealBoxService().get(MealBoxDto.class, id);
				if (mealBoxDto != null) {
					getSession().setAttribute("imagePath", mealBoxDto.getImages());
				}
			} else {
				getSession().setAttribute("imagePath", null);
			}
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		
		return "meal-box";
	}
	
	public String updateMealBox() throws Exception{
		
		String id = ServletActionContext.getRequest().getParameter("id");//包间的编号
		if(PublicUtil.isNotNullAndEmpty(id)){
			mealBoxDto = (MealBoxDto)super.serviceFacade.getMealBoxService().get(MealBoxDto.class, Integer.valueOf(id));
			getSession().setAttribute("mealBoxDto", mealBoxDto);
		}

		return "update-meal-box";
	}
	
	/**
	 * 保存和修改包厢信息
	 * @return
	 */
	public String mealBoxSaveOrUpdate() {
		try {
			Object img = getSession().getAttribute("imagePath");
			if (img != null) {
				images = img.toString();
			}
			//MealBoxDto mbd = new MealBoxDto(name, images, type, peopleNumber, lowConsume, remark, status, getRestaurantDto().getId(),getUser().getUserName(),DateUtil.getDatePatternM(),getUser().getUserName(),DateUtil.getDatePatternM());
			MealBoxDto mbd = new MealBoxDto();
			mbd.setName(name);
			mbd.setImages(images);
			mbd.setPeopleNumber(peopleNumber);
			mbd.setType(type);
			mbd.setLowConsume(lowConsume);
			mbd.setRemark(remark);
			
			mbd.setRestaurantId(null==getSession().getAttribute("restaurantId")?null:Integer.valueOf(getSession().getAttribute("restaurantId").toString()));
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session
			
			String name = "";//修改人姓名  管理员或者餐厅管理员
			
			if(null != adminUserDto){
				name = getAdminUser().getUserName();
			
			}else if(null != restaurentUserDto){
				name = getRestaurentUserDto().getAccount();
				
			}
			
			//修改操作
			if (id != null && 0 != id) {
				mbd.setUpdateTime(DateUtil.getDatePatternM());
				mbd.setUpdateUser(name);
				mbd.setId(id);
				super.serviceFacade.getMealBoxService().update(mbd);
			} else {//保存操作
				List restList = null;
				
				if(null != adminUserDto){
					
					restList = super.serviceFacade.getRestaurantService().findByRestaurant(getAdminUser().getId());
				}else if(null != restaurentUserDto){
					
					restList = super.serviceFacade.getRestaurantService().findByRestaurant(getRestaurentUserDto().getId());
				}
				
				//RestaurantDto restDto = (RestaurantDto)
				RestaurantDto restDto = null;
				if(restList.size()>0){
					
					restDto = (RestaurantDto)restList.get(0);
					if (restDto != null) {
						mbd.setRestaurantId(restDto.getId());
						mbd.setCreateTime(DateUtil.getDatePatternM());
						mbd.setCreateUser(name);
					}
				}
				super.serviceFacade.getMealBoxService().save(mbd);
			}
			getSession().setAttribute("imagePath", null); 
			getSession().removeAttribute("imagePath");
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		requestURL = "mealbox!mealBoxList.html";
		return SUCCESS;
	}
	
	/**
	 * 删除包厢信息
	 * @return
	 */
	public String mealBoxDelete() {
		try {
			MealBoxDto mbd = new MealBoxDto();
			mbd.setId(id);
			super.serviceFacade.getMealBoxService().delete(mbd);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImages() {
		return images;
	}

	public void setImages(String images) {
		this.images = images;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getPeopleNumber() {
		return peopleNumber;
	}

	public void setPeopleNumber(Integer peopleNumber) {
		this.peopleNumber = peopleNumber;
	}

	public Double getLowConsume() {
		return lowConsume;
	}

	public void setLowConsume(Double lowConsume) {
		this.lowConsume = lowConsume;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public List<MealBoxDto> getMealBoxList() {
		return mealBoxList;
	}

	public void setMealBoxList(List<MealBoxDto> mealBoxList) {
		this.mealBoxList = mealBoxList;
	}

	public MealBoxDto getMealBoxDto() {
		return mealBoxDto;
	}

	public void setMealBoxDto(MealBoxDto mealBoxDto) {
		this.mealBoxDto = mealBoxDto;
	}

	public RestaurantDto getRestaurantDto() {
		return restaurantDto;
	}

	public void setRestaurantDto(RestaurantDto restaurantDto) {
		this.restaurantDto = restaurantDto;
	}
}
